package com.hibase.upms.controller;


import com.hibase.common.constant.RedisKeyConstant;
import com.hibase.common.constant.ResponseCode;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.entity.User;
import com.hibase.common.exception.HibaseException;
import com.hibase.common.util.EntityUtil;
import com.hibase.common.util.UserContext;
import com.hibase.core.redis.util.RedisUtil;
import com.hibase.core.web.annotation.HibaseRequestMapping;
import com.hibase.upms.entity.login.user.vo.LoginUserVO;
import com.hibase.upms.entity.user.vo.MdsUserVO;
import com.hibase.upms.service.login.ILoginService;
import com.hiibase.core.security.cache.Cache;
import com.hiibase.core.security.properties.JwtProperties;
import com.hiibase.core.security.provider.JwtProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hufeng
 * @create 2018-09-12 13:53
 */
@Api(tags = "登陆管理")
@Controller
@RequestMapping("login")
@Slf4j
public class LoginController{

    @Autowired
    private ILoginService loginService;

    @Autowired
    private Cache userCache;

    @Autowired
    private JwtProvider jwtProvider;

    @Autowired
    private JwtProperties jwtProperties;
    
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "用户登录",httpMethod = "POST")
    @HibaseRequestMapping(value = "login", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseModel login(@RequestBody LoginUserVO loginUserVO, HttpServletRequest request) {
        String token = null;
        try {
        	 token = loginService.checkLoginUser(request,loginUserVO.getLoginName(), loginUserVO.getLoginPwd());
        	 return ResponseModel.successMsg(token);
		} catch (Exception e) {
	    	log.error("登录失败");
	    	throw new HibaseException(ResponseCode.ADMIN_USER_PWD_ERROR.getCode(), e.getMessage());
	    }
    }


    @ApiOperation(value = "退出登录",httpMethod = "POST")
    @HibaseRequestMapping(value = "logout", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseModel logout(HttpServletRequest request) {

    	User user = UserContext.get();
    	//查询key存不存在
    	Boolean hasKey = redisUtil.hasKey(RedisKeyConstant.CACHE_USER_XML_MENU_KEY + user.getId() + ":");
    	if (hasKey) {
    		//清除用户MENU XML
    		redisUtil.delete(RedisKeyConstant.CACHE_USER_XML_MENU_KEY + user.getId() + ":");
		}
        UserContext.clear();
        userCache.remove(jwtProvider.getToken(request).substring(jwtProperties.getTokenPrefix().length()));
        SecurityContextHolder.clearContext();
        log.debug("用户：" + user.getLoginName() + ", 退出登录成功");
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "检查是否已经登陆", httpMethod = "POST",response = MdsUserVO.class)
    @HibaseRequestMapping(value = "checkLogin", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseModel checkLogin() {

        User user = UserContext.get();

        if (user == null) {

            return ResponseModel.failMsg(ResponseCode.USER_NOTLOGIN.getCode(), ResponseCode.USER_NOTLOGIN.getMessage());
        } else {

            return ResponseModel.successMsg(EntityUtil.transform(user, MdsUserVO.class));
        }
    }







}
